<template>
	<view class="bottomShare">
		<view class="shareTip" v-if="shareTip == true">
			<view class="mask" @click="showShareTip"></view>
			<image class="img" src="/static/public/images/share/tip2.png" mode="widthFix"></image>
		</view>
		<view class="btn_box">
			<u-grid :col="isWeixn?3:2" class="u-grid" :border="false">
				<u-grid-item @click="copy">
					<view class="btn">
						<image class="img" src="/static/public/images/share/icon_01.png"></image>
					</view>
					<view>复制链接</view>
				</u-grid-item>
				<!-- <u-grid-item v-if="isWeixn" @click="showShareTip">
					<view class="btn">
						<image class="img" src="/static/public/images/share/icon_02.png"></image>
					</view>
					<view>微信分享</view>
				</u-grid-item> -->
				<u-grid-item @click="save" v-if="openSettingBtnHidden">
					<view class="btn">
						<image class="img" src="/static/public/images/share/icon_03.png"></image>
					</view>
					<view>保存图片</view>
				</u-grid-item>
				<u-grid-item  v-else @click="openSet">
					<view class="btn">
						<image class="img" src="/static/public/images/share/icon_03.png"></image>
					</view>
					<view>保存图片</view>
				</u-grid-item>
			</u-grid>
		</view>
	</view>
</template>

<script>
	let isWeixn = false;
	//#ifdef H5
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i)=="micromessenger") {
		isWeixn =  true;
	} else {
		isWeixn = false;
	}
	//#endif
	export default {
		name: "bottomShare",
		props: {
			url: {
				type: String,
				default: '',
			},
			imgFile: {
				type: String,
				default: '',
			},
			share_url:{
				type:String,
				default:''
			}
		},
		data() {
			return {
				openSettingBtnHidden: true,//是否授权
				shareTip:false,
				isWeixn: isWeixn
			}
		},
		methods: {
			showShareTip(){
				this.shareTip = !this.shareTip;
			},
			openSet(){
				let _this = this;
				uni.openSetting({
				  success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							_this.openSettingBtnHidden = false;
						} else {
							_this.openSettingBtnHidden = true;
						}
					}
				});
			},
			copy(){
				let _this = this;
				uni.setClipboardData({ data:_this.share_url, success:function(data){
					_this.$u.toast('复制成功.');
				}, fail:function(err){
					_this.$u.toast('复制失败，请重试.');
				}, complete:function(res){} })
			},
			save(){
				let _this = this;
				if (this.imgFile == ''){
					return false;
				}
				/* #ifdef H5 */
					_this.$u.toast('长按图片，保存图片.');
					return false;
				/* #endif */
				
				/* #ifdef MP-WEIXIN */
					_this.saveEwm();
					return false;
				/* #endif */
				uni.saveImageToPhotosAlbum({ filePath: _this.imgFile, success: function () {
					_this.$u.toast('保存成功.');
				}, fail:function(err){
					_this.app.showToast('长按图片，保存图片.');
				}, complete:function(res){} })
			},
			saveEwm:function(e){
				let _this = this;
				//获取相册授权
				uni.getSetting({
					success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
								 //这里是用户同意授权后的回调
									_this.saveImgToLocal();
								},
								fail() {//这里是用户拒绝授权后的回调
									_this.openSettingBtnHidden = false;
									_this.app.showToast('保存失败.');
								}
							})
						} else {//用户已经授权过了
							_this.saveImgToLocal();
						}
					}
				})
			},
			saveImgToLocal:function(e){
				let _this = this;
				uni.downloadFile({
					url: _this.imgFile,//图片地址
					success: (res) =>{
						if (res.statusCode === 200){
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									_this.app.showToast('保存成功.');
								},
								fail: function() {
									_this.app.showToast('保存失败.');
								}
							});
						} 
					}
				}) 
			}
		}
	}
</script>

<style lang="scss">
	.bottomShare{
		height: 220rpx;
		.shareTip {
			position: fixed;
			top: 0rpx;
			right: 0;
			width: 100%;
			text-align: right;
			height: 100%;
			z-index: 999;
			background: rgba(0, 0, 0, .5);
			.mask {
				position: absolute;
				z-index: 999;
				width: 100%;
				height: 100%;
			}
			.img{
				width: 100%;
				margin-top: 20rpx;
			}
		}
		.btn_box {
			position: fixed;
			bottom: 0px;
			width: 100%;
			.u-grid-item {
				background: none !important;
			}
			.btn {
				border-radius: 100%;
				width: 120rpx;
				height: 120rpx;
				background: #fff;
				text-align: center;
				margin-bottom: 20rpx;
				border:1rpx solid #cccccc;
				.img {
					padding-top: 28rpx;
					width: 56rpx;
					height: 56rpx;
				}
			}
		}
	}
</style>
